import { useUserManagerStore } from './userManagerStore'
import { useUserStore } from './userStore'
import { ApiEndpoints } from '~/types/ApiEndpoints'
import { IUser } from '~/types/User'

interface IState {
  contactList: IUser[]
  contactListPending: boolean
}

export const useContactsStore = defineStore('contactsStore', {
  state: () =>
    ({
      contactList: [],
      contactListPending: false
    } as IState),
  actions: {
    async getContactList(filter: any) {
      const config = useRuntimeConfig()
      const userManagerStore = useUserManagerStore()
      const userStore = useUserStore()
      this.contactListPending = true
      this.contactList = await $fetch(ApiEndpoints.GET_ALL_USERS, {
        method: 'POST',
        baseURL: config.public.baseURL,
        headers: [
          [
            'Authorization',
            'Bearer ' + (userManagerStore.user?.access_token || '')
          ]
        ],
        body: { ...filter },
        // PP-99
        query: {
          orgUUID: userStore.user.orgUuid
        }
      })
      this.contactListPending = false
    }
  }
})
